package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CartBean;
import com.project.bean.DetailBean;
import com.project.bean.OrderBean;
import com.project.bean.UserBean;
import com.project.mapper.*;
import com.project.service.IOrderService;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private ICartMapper cartMapper;
    @Autowired
    private IDetailMapper detailMapper;
    @Autowired
    private IUserMapper userMapper;

    @Override
    public void add(OrderBean order) {
        //计算购物车总价
        Integer sumPrice = cartMapper.computeCartPrice(order.getUserId());
        order.setSumPrice(sumPrice);
        //添加订单
        orderMapper.insert(order);
        //查询登录用户的购物车集合
        QueryWrapper<CartBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", order.getUserId());
        List<CartBean> cartList = cartMapper.selectList(qw);
        //批量添加订单明细
        detailMapper.addMore(order.getId(), cartList);
        //清空购物车
        cartMapper.delete(qw);

    }

    @Override
    public IPage<OrderBean> cutByItem(Integer pageNO, Integer userId, String status, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        UserBean user = userMapper.selectById(userId);
        if (userId != null && user.getGrade().equals("顾客")) {
            qw.eq("fk_userId", userId);
        }
        if (status != null) {
            qw.eq("o_status", status);
        }
        if (startDate != null) {
            qw.ge("o_orderDate", startDate);
        }
        if (endDate != null) {
            qw.le("o_orderDate", endDate);
        }

        return orderMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public OrderBean findByOrderId(Integer orderId) {
        OrderBean order = orderMapper.selectById(orderId);
        List<DetailBean> detailList = detailMapper.findByOrderId(orderId);
        order.setDetailList(detailList);
        return order;
    }

    @Override
    public void send(Integer orderId) {
        OrderBean order = orderMapper.selectById(orderId);
        order.setStatus("发货中");
        orderMapper.updateById(order);
    }

    @Override
    public List<OrderBean> findByStatus() {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        qw.eq("o_status", "发货中");
        return orderMapper.selectList(qw);
    }

    @Override
    public void compile(Integer orderId) {
        OrderBean order = orderMapper.selectById(orderId);
        order.setStatus("已完结");
        orderMapper.updateById(order);
    }
}
